<template>
  <scroller class="listview" @scroll="scroll"
            :listen-scroll="listenScroll"
            :probe-type="probeType"
            :data="data"
            ref="listview">
    <ul>
      <li v-for="item in data" class="list-group" ref="listGroup">
        <ul>
          <h3 class="list-group-title">{{item.title}}</h3>
          <li v-for="sub in item.item" class="list-group-item" @click.stop.prevent="select(sub)">
            <img class="avatar" v-lazy="sub.avatar">
            <span class="name">{{sub.name}}</span>
          </li>
        </ul>
      </li>
    </ul>
    <!--右边索引-->
    <div class="list-shortcut" @touchstart.prevent.stop="shortcutTouchstart"
         @touchmove.stop.prevent="onShortcutTouchMove" @touchend.stop>
      <ul>
        <li class="shortcut-item" v-for="(item, i) in indexList" :class="{'current': currentIndex==i}" :data-index="i">
          {{item}}
        </li>
      </ul>
    </div>
    <!--固定的标题-->
    <div class="list-fixed" ref="fixed" v-show="fixedTitle">
      <div class="fixed-title">{{fixedTitle}} </div>
    </div>
    <div v-show="!data.length" class="loading-container">
      <loading></loading>
    </div>
  </scroller>
</template>

<script>
  import Scroller from '../scroll/scroll.vue';
  import Loading from '../loading/loading.vue';
  import {handleAttr} from 'common/js/dom.js';

  const ANCHOR_HEIGHT = 18;
  const TITLE_HEIGHT = 30;
  export default {
    components: {Scroller, Loading},
    props: {
      data: {type: Array, default: []}
    },
    data () {
      return {
        currentIndex: 0,
        touch: {},
        scrollY: -1,
        listHeight: [],
        diff: -1
      };
    },
    computed: {
      indexList () {
        return this.data.map((group) => {
          return group.title.substring(0, 1);
        });
      },
      fixedTitle () {
        if (this.scrollY > 0) {
          return '';
        }
        let title = this.data[this.currentIndex] ? this.data[this.currentIndex].title : '';
        //  console.log(`fixed:${title},index:${this.currentIndex}`);
        return title;
      }
    },
    watch: {
      data () {
        setTimeout(() => {
          this._calculateListHeight();
        }, 20);
      },
      scrollY (newY) {
        const listHeight = this.listHeight;
        if (newY >= 0) { // 在顶部
          this.currentIndex = 0;
          return;
        }
        // 在中间
        for (let i = 0; i < listHeight.length - 1; i++) {
          let h1 = listHeight[i];
          let h2 = listHeight[i + 1];
          if (-newY >= h1 && -newY < h2) {
            this.currentIndex = i;
            // console.log(this.currentIndex + ' ---');
            this.diff = h2 + newY;
            return;
          }
        }
        // 底部上限
        this.currentIndex = listHeight.length - 2;
      },
      diff (newVal) {
        let fixedTop = (newVal > 0 && newVal < TITLE_HEIGHT) ? newVal - TITLE_HEIGHT : 0;
        if (this.fixedTop === fixedTop) {
          return;
        }
        this.fixedTop = fixedTop;
        this.$refs.fixed.style.transform = `translate3d(0,${fixedTop}px,0)`;
      }
    },
    methods: {
      select (singer) {
        this.$emit('select', singer);
      },
      shortcutTouchstart (e) {
        let anchorIndex = handleAttr(e.target, 'index');
        let firstTouch = e.touches[0];
        this.touch.y0 = firstTouch.pageY;
        this.touch.anchorIndex = anchorIndex;
        this._scrollTo(anchorIndex);
//        console.log(anchorIndex);
        this.currentIndex = anchorIndex;
      },
      onShortcutTouchMove (e) {
        let touch = e.touches[0];
        this.touch.y1 = touch.pageY;
        let diffIndex = ((this.touch.y1 - this.touch.y0) / ANCHOR_HEIGHT) | 0;
        this._scrollTo(diffIndex);
        this.currentIndex = diffIndex;
      },
      refresh () {
        this.$refs.listview.refresh();
      },
      scroll (pos) { // 滚动时会触发
//        console.log(pos);
        this.scrollY = pos.y;
      },
      _calculateListHeight () {
        this.listHeight = [];
        const list = this.$refs.listGroup;
        let height = 0;
        for (let i = 0; i < list.length; i++) {
          let item = list[i];
          const h = item.clientHeight;
          height += h;
          this.listHeight.push(height);
        }
      },
      _scrollTo (index) {
        if (!index && index !== 0) {
          return;
        }
        if (this.listHeight.length === 0) {
          this._calculateListHeight();
        }
        if (index < 0) {
          index = 0;
        } else if (index > this.listHeight.length - 2) {
          index = this.listHeight.length - 2;
        }
//        console.log(`scrollTo:${index}`);
        this.scrollY = -this.listHeight[index];
        this.$refs.listview.scrollToElement(this.$refs.listGroup[index], 0);
      }
    },
    created () {
      this.probeType = 3;
      this.listenScroll = true;
      this.touch = {};
      this.listHeight = [];
    }
  };
</script>

<style lang="less" scoped="">
  @import '~common/css/variable.less';

  .listview {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: @color-background;
    .list-group {
      padding-bottom: 30px;
      .list-group-title {
        height: 30px;
        line-height: 30px;
        padding-left: 20px;
        font-size: @font-size-small;
        color: @color-text-l;
        background: @color-highlight-background;
      }
      .list-group-item {
        display: flex;
        align-items: center;
        padding: 20px 0 0 30px;
        .avatar {
          width: 50px;
          height: 50px;
          border-radius: 50%;
        }
        .name {
          margin-left: 20px;
          color: @color-text-l;
          font-size: @font-size-medium;
        }
      }
    }
    .list-shortcut {
      position: absolute;
      right: 0;
      top: 50%;
      width: 20px;
      transform: translateY(-50%);
      text-align: center;
      background: @color-background-d;
      .shortcut-item {
        padding: 3px;
        font-size: @font-size-small;
        line-height: 1;
        color: @color-text-l;
        &.current {
          color: @color-theme;
        }
      }
    }
    .list-fixed {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      .fixed-title {
        height: 30px;
        line-height: 30px;
        padding-left: 20px;
        font-size: @font-size-small;
        color: @color-text-l;
        background: @color-highlight-background;
      }
    }
    .loading-container {
      position: absolute;
      width: 100%;
      top: 50%;
      transform: translateY(-50%);
    }
  }
</style>
